package com.white.androidstudy.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;

import com.white.androidstudy.R;
import com.white.androidstudy.widget.AbsView;

/**
 * Created by ytf on 2017/9/28 028.
 * Description: 星级
 */

public class MyRattingStar extends AbsView
{
    public MyRattingStar(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        init();
    }

    private void init()
    {
        bmpEmpty = ((BitmapDrawable)getResources().getDrawable(R.drawable.ic_star_empty)).getBitmap();
        bmpEntity = ((BitmapDrawable)getResources().getDrawable(R.drawable.ic_star_checked)).getBitmap();
    }

    Bitmap bmpEmpty, bmpEntity;

    @Override
    public int getDefaultWrapWidth()
    {
        return 195;
    }

    @Override
    public int getDefaultWrapHeight()
    {
        return 36;
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
        drawBg(canvas);
        drawProgress(canvas);
    }

    private void drawProgress(Canvas canvas)
    {
        for(int i = 0;i<star;i++)
        {
            int x = i*39;
            canvas.drawBitmap(bmpEntity,x, 0,null);
        }
    }

    private void drawBg(Canvas canvas)
    {
        for(int i = 0;i<5;i++)
        {
            int x = i*39;
            canvas.drawBitmap(bmpEmpty, x, 0, null);
        }
    }

    int star = 3;
    public void setStar(int s)
    {
        star = s;
        if(star > 5)
            star = 5;
        invalidate();
    }

    @Override
    public void onGlobalLayout()
    {

    }
}
